#include <iostream>
#include <thread>
#include <mutex>

using namespace std;
mutex m;

void proc1(int a)
{
    //线程1
    //先上锁，
    m.lock();
    //adopt_lock确实是跟收养了个锁差不多，
    //然后托管给lock_guard这个类，之后如果发生了异常阿也不需要没有解锁而产生死锁的现象
    lock_guard<mutex> g1(m,adopt_lock);
    cout <<"proc1"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc1============================"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc1============================"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc1============================"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc1============================"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc1============================"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout<<"proc1结束"<<endl;
}

void proc2(int a)
{   //线程2
    lock_guard<mutex> g2(m);
    cout <<"proc2"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc2-------------------------------"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc2-------------------------------"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc2-------------------------------"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc2-------------------------------"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc2-------------------------------"<<endl;
    this_thread::sleep_for(chrono::seconds(1));
    cout <<"proc2-------------------------------"<<endl;
    cout<<"proc2结束"<<endl;
}

int main()
{
    int a=0;
    thread th1(proc1,a);
    thread th2(proc2,a);
    th1.join();
    th2.join();
    return 0;
}
